﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI.WebControls;
using System.Data;
using DreamBuilder.Core.Container;
using DreamBuilder.DictionaryAccess;
using DreamBuilder.UI.UserControls.DataAccess.Services;

namespace DreamBuilder.UI.UserControls
{
    /// <summary>
    /// CLable 的摘要说明。
    /// </summary>
    public class LabelEdit : EditItemBase
    {
        Label label = null;
        VoucherEditItemInfo voucherEditItemInfo = null;
       public LabelEdit(EditArgs editArgs)
       {
           label = new Label();
           voucherEditItemInfo = DictionaryAccessFacade.GetVoucherEditItemInfo(editArgs.ID, editArgs.ItemCode);
           base.InstantiateIn(editArgs);
       }
       public override string Value
       {
           get
           {
               return this.label.Text.Trim(); ;
           }
       }
       public override System.Web.UI.Control Control
       {
           get
           {
               return label;
           }
       }
       protected override void SetControlID()
       {
           label.ID = "lbl_" + voucherEditItemInfo.TableCode + "__" + voucherEditItemInfo.FieldCode;
       }
       protected override void SetControlValue(EditArgs editArgs)
       {
           string sResult = "";
           DataTable dt = editArgs.EditData;
           string columnName = voucherEditItemInfo.TableCode + "__" + voucherEditItemInfo.FieldCode;
           //判断数据表是否有值
           if (dt.Rows.Count > 0 && dt.Rows[0][columnName] != System.DBNull.Value)
           {
               FieldDicInfo fieldDicInfo = DictionaryAccessFacade.GetFieldDicInfo(voucherEditItemInfo.TableCode, voucherEditItemInfo.FieldCode);

               //这里如果是参照自身转换
               if (fieldDicInfo.FieldType == FIELDTYPE.TYPE_Reference)
               {
                   UCControlCommon controlCommon = UContainer.Resolve<UCControlCommon>();
                   IDataReader iDataReader = controlCommon.GetRefTextReader(fieldDicInfo.ReferenceObj, dt.Rows[0][columnName].ToString());
                   if (iDataReader.Read())
                   {
                       sResult = iDataReader.GetValue(0).ToString();
                   }
                   else
                   {
                       sResult = string.Empty;
                   }
                   iDataReader.Close();
               }
               else if (fieldDicInfo.FieldType == FIELDTYPE.TYPE_Date)
               {
                   sResult = DateTime.Parse(dt.Rows[0][columnName].ToString()).ToShortDateString();
               }
               else
               {
                   sResult = dt.Rows[0][columnName].ToString();
               }

           }
           //如果没有初始值
           if (sResult.Trim().Length > 0)
           {
               label.Text = sResult;
           }
           else
           {
               switch (voucherEditItemInfo.InitValue.ToLower())
               {
                   case "[today]":
                       label.Text = DateTime.Now.ToString(@"yyyy-MM-dd");
                       break;
                   case "[now]":
                       label.Text = DateTime.Now.ToString(@"yyyy-MM-dd HH:mm:ss");
                       break;
                   default:
                       label.Text = voucherEditItemInfo.InitValue;
                       break;
               }
           }
       }

       protected override void SetControlAttributes(EditArgs editArgs)
       {
           FieldDicInfo fieldDicInfo = DictionaryAccessFacade.GetFieldDicInfo(voucherEditItemInfo.TableCode, voucherEditItemInfo.FieldCode);
           label.Width = Unit.Percentage(100);
           int nRows = voucherEditItemInfo.Rows;
           if (nRows > 1)
           {
               label.CssClass = "ED-readOnly";
               label.Height = nRows * 23;     //行合并时，高度=行数*23px
           }
           else
           {
               label.CssClass = "ED-readOnly";
           }
           label.Enabled = (!voucherEditItemInfo.ReadOnly);
       }	

    }
}
